﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TD.bom;

namespace TD.Application
{
    /// <summary>
    /// Gère le code spécifique à la phase de dev
    /// </summary>
    public class DeveloperHelper
    {
        protected int m_loopCount;
        protected Label m_fps;
        protected Label m_mapTemplate;
        protected Label m_inGameTime;
        DateTime m_lastTick;

        protected static DeveloperHelper m_instance;

        /// <summary>
        /// Contrôle hébergeant le jeu
        /// </summary>
        protected GameForm m_gameForm;


        /// <summary>
        /// Unique instance de la classe (singleton)
        /// </summary>
        public static DeveloperHelper Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new DeveloperHelper();
                }

                return m_instance;
            }
        }

        /// <summary>
        /// Constructeur protégé (singleton)
        /// </summary>
        protected DeveloperHelper()
        {
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="l_gameForm">Contrôle hébergeant le jeu</param>
        public void Initialize(GameForm l_gameForm)
        {
            m_loopCount = 0;

            StackPanel l_panel = new StackPanel();
            l_panel.Name = "DebugLayout";
            l_panel.Background = new SolidColorBrush(Color.FromArgb(255, 200, 100, 200));

            m_fps = new Label();
            m_fps.Name = "m_fps";
            l_panel.Children.Add(m_fps);

            m_inGameTime = new Label();
            m_inGameTime.Name = "m_inGameTime";
            l_panel.Children.Add(m_inGameTime);

            Button m_pauseResumeClock = new Button();
            m_pauseResumeClock.Name = "m_pauseResumeClock";
            m_pauseResumeClock.Content = "None";
            m_pauseResumeClock.Click += new RoutedEventHandler(OnPauseResumeClockClick);
            l_panel.Children.Add(m_pauseResumeClock);

            m_mapTemplate = new Label();
            m_mapTemplate.Name = "m_mapTemplate";
            l_panel.Children.Add(m_mapTemplate);

            l_gameForm.ControlsLayout.Children.Add(l_panel);

            // Active le compteur fps de SL
            System.Windows.Application.Current.Host.Settings.EnableFrameRateCounter = true;
        }

        /// <summary>
        /// Click sur le button de mise en pause de l'horloge
        /// </summary>
        private void OnPauseResumeClockClick(object sender, RoutedEventArgs e)
        {
            if (Game.Instance.Clock.Paused)
            {
                Game.Instance.Clock.Resume();
                ((Button)sender).Content = "Pause";
            }
            else
            {
                Game.Instance.Clock.Pause();
                ((Button)sender).Content = "Resume";
            }
        }

        /// <summary>
        /// Appelé lors du rendu
        /// </summary>
        public void OnGameFormRendering()
        {
            // Calcul du frame rate
            ++m_loopCount;
            TimeSpan l_elapsedTime = DateTime.Now - m_lastTick;
            if (l_elapsedTime.TotalSeconds > 1)
            {
                m_fps.Content = string.Format("fps : {0:d}", (int)(m_loopCount / l_elapsedTime.TotalSeconds));
                m_lastTick = DateTime.Now;
                m_loopCount = 0;
            }

            if (Game.Instance.Clock != null)
            {
                m_inGameTime.Content = "time : " + Game.Instance.Clock.Time.ToString();
            }
            else
            {
                m_inGameTime.Content = "None";
            }

            m_mapTemplate.Content = Game.Instance.Map != null ? "map : " + Game.Instance.Map.TemplateName : "No map template";
        }
    }
}
